//
//  ToolDetailViewController.m
//  TravelEveryDay
//
//  Created by 路洪宇 on 15/10/24.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "ToolDetailViewController.h"
#import "MyLocationModel.h"
#import "BaseHeader.h"
@interface ToolDetailViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *cNYTextField;
@property(nonatomic,strong)UITextField *uSDTextField;
@property(nonatomic,strong)UITextField *eURTextField;
@property(nonatomic,strong)UITextField *changeTextField;

@property(nonatomic,strong)NSMutableArray *unitArray;

@property(nonatomic,assign)CGFloat cNYNum;
@property(nonatomic,assign)CGFloat uSDNum;
@property(nonatomic,assign)CGFloat eURNum;
@property(nonatomic,assign)CGFloat changeNum;


@property(nonatomic,strong)NSString *uSDUnit;
@property(nonatomic,strong)NSString *eURUnit;
@property(nonatomic,strong)NSString *changeURUnit;


@property(nonatomic,strong)NSString *changeMoney;



@end

@implementation ToolDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    _unitArray = [[NSMutableArray alloc] init];
    [self setUpTitleView];
    [self getCurrencyUnit];
    _cNYNum = 0;
    _uSDNum = 0;
    _eURNum = 0;
}

-(void)setUpTitleView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [titleView setBackgroundColor:TITLECOLOR];
    [self.view addSubview:titleView];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(10, 30, 22, 22)];
    [backButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 25, 200, 30)];
    [title setText:@"实时汇率"];
    [title setTextColor:[UIColor whiteColor]];
    title.font = [UIFont systemFontOfSize:19.];
    title.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:title];
    
    
    [self.view addSubview:titleView];
    [self setUpExchangeView];
    
}


-(void)getCurrencyUnit
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"CurrencyUnit" ofType:@"plist"];
    
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    NSMutableArray *dataArray = [infoDic objectForKey:@"CurrencyUnit"];
    
    for (NSDictionary *dic in dataArray) {
        
        MyLocationModel *model = [MyLocationModel modelWithDictionary:dic];
        [_unitArray addObject:model];
    }
    

}
-(void)setUpExchangeView
{
    [self getCurrencyUnit];

    //改变行
    UIImageView *chageImgae = [[UIImageView alloc] initWithFrame:CGRectMake(10, 84, 50, 50)];
    NSLog(@"%@",self.cityStr);
    chageImgae.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@@2x.png",self.cityStr]];
    [self.view addSubview:chageImgae];
    
    UILabel *changeLabel = [[UILabel alloc] initWithFrame:CGRectMake(chageImgae.frame.size.width+20, chageImgae.frame.origin.y, 100, 50)];
    changeLabel.text = self.cityStr;
    changeLabel.textColor = [UIColor blackColor];
    changeLabel.font = [UIFont systemFontOfSize:25.];
    [self.view addSubview:changeLabel];
    
    _changeTextField = [[UITextField alloc] initWithFrame:CGRectMake(changeLabel.frame.size.width+changeLabel.frame.origin.x+10, changeLabel.frame.origin.y, SCREEN_WIDTH-(changeLabel.frame.size.width+changeLabel.frame.origin.x+40), 50)];
    _changeTextField.textAlignment = NSTextAlignmentRight;
    _changeTextField.keyboardType = UIKeyboardTypeNumberPad;
    //UITextField设置placeholder颜色
    UIColor *color = [UIColor blackColor];
    _changeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_cNYNum]attributes:@{NSForegroundColorAttributeName: color}];
    _changeTextField.font = [UIFont boldSystemFontOfSize:25.];
    _changeTextField.delegate = self;
    _changeTextField.adjustsFontSizeToFitWidth = YES;
    
    [_changeTextField addTarget:self action:@selector(changeUnitDelegate) forControlEvents:UIControlEventEditingChanged];
    
    [self.view addSubview:_changeTextField];
    
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.EnName isEqual:self.cityStr]) {
            _changeMoney = tempModel.FullName;
        }
    }
    UILabel *changeCNY = [[UILabel alloc] initWithFrame:CGRectMake(changeLabel.frame.size.width+changeLabel.frame.origin.x+10, _changeTextField.frame.origin.y+25, SCREEN_WIDTH-(changeLabel.frame.size.width+changeLabel.frame.origin.x+40), 50)];
    changeCNY.text = _changeMoney;
    changeCNY.textAlignment = NSTextAlignmentRight;
    changeCNY.font = [UIFont systemFontOfSize:12.];
    changeCNY.textColor = [UIColor lightGrayColor];
    [self.view addSubview:changeCNY];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10,changeCNY.frame.size.height+changeCNY.frame.origin.y , SCREEN_WIDTH-20, 0.5)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.7;
    [self.view addSubview:lineView];
    
    //第一行
    UIImageView *cNYImgae = [[UIImageView alloc] initWithFrame:CGRectMake(10, 84+90, 50, 50)];
    cNYImgae.image = [UIImage imageNamed:@"CNY@2x.png"];
    [self.view addSubview:cNYImgae];
    
    UILabel *cNYlabel = [[UILabel alloc] initWithFrame:CGRectMake(cNYImgae.frame.size.width+20, cNYImgae.frame.origin.y, 100, 50)];
    cNYlabel.text = @"CNY";
    cNYlabel.textColor = [UIColor blackColor];
    cNYlabel.font = [UIFont systemFontOfSize:25.];
    [self.view addSubview:cNYlabel];
    
    _cNYTextField = [[UITextField alloc] initWithFrame:CGRectMake(cNYlabel.frame.size.width+cNYlabel.frame.origin.x+10, cNYImgae.frame.origin.y, SCREEN_WIDTH-(cNYlabel.frame.size.width+cNYlabel.frame.origin.x+40), 50)];
    _cNYTextField.textAlignment = NSTextAlignmentRight;
    _cNYTextField.keyboardType = UIKeyboardTypeNumberPad;
    //UITextField设置placeholder颜色
    _cNYTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_cNYNum]attributes:@{NSForegroundColorAttributeName: color}];
    _cNYTextField.font = [UIFont boldSystemFontOfSize:25.];
    _cNYTextField.delegate = self;
    _cNYTextField.adjustsFontSizeToFitWidth = YES;

    [_cNYTextField addTarget:self action:@selector(chineseUnitDelegate) forControlEvents:UIControlEventEditingChanged];
    
    [self.view addSubview:_cNYTextField];
    
    UILabel *chineseCNY = [[UILabel alloc] initWithFrame:CGRectMake(cNYlabel.frame.size.width+cNYlabel.frame.origin.x+10, _cNYTextField.frame.origin.y+25, SCREEN_WIDTH-(cNYlabel.frame.size.width+cNYlabel.frame.origin.x+40), 50)];
    chineseCNY.text = @"人民币";
    chineseCNY.textAlignment = NSTextAlignmentRight;
    chineseCNY.font = [UIFont systemFontOfSize:12.];
    chineseCNY.textColor = [UIColor lightGrayColor];
    [self.view addSubview:chineseCNY];
    
    UIView *lineView4 = [[UIView alloc] initWithFrame:CGRectMake(10,chineseCNY.frame.size.height+chineseCNY.frame.origin.y , SCREEN_WIDTH-20, 0.5)];
    lineView4.backgroundColor = [UIColor lightGrayColor];
    lineView4.alpha = 0.7;
    [self.view addSubview:lineView4];
    
    //第二行
    UIImageView *uSDImgae = [[UIImageView alloc] initWithFrame:CGRectMake(10, 84+90*2, 50, 50)];
    uSDImgae.image = [UIImage imageNamed:@"USD@2x.png"];
    [self.view addSubview:uSDImgae];
    
    UILabel *uSDlabel = [[UILabel alloc] initWithFrame:CGRectMake(uSDImgae.frame.size.width+20, uSDImgae.frame.origin.y, 100, 50)];
    uSDlabel.text = @"USD";
    uSDlabel.textColor = [UIColor blackColor];
    uSDlabel.font = [UIFont systemFontOfSize:25.];
    [self.view addSubview:uSDlabel];
    
    _uSDTextField = [[UITextField alloc] initWithFrame:CGRectMake(uSDlabel.frame.size.width+uSDlabel.frame.origin.x+10, uSDImgae.frame.origin.y, SCREEN_WIDTH-(uSDlabel.frame.size.width+uSDlabel.frame.origin.x+40), 50)];
    _uSDTextField.textAlignment = NSTextAlignmentRight;
       _uSDTextField.adjustsFontSizeToFitWidth = YES;
    //UITextField设置placeholder颜色
    _uSDTextField.keyboardType = UIKeyboardTypeNumberPad;
    _uSDTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_uSDNum]attributes:@{NSForegroundColorAttributeName: color}];
    _uSDTextField.font = [UIFont boldSystemFontOfSize:25.];
    [_uSDTextField addTarget:self action:@selector(uSDUnitDelegate) forControlEvents:UIControlEventEditingChanged];

    _uSDTextField.delegate = self;
    [self.view addSubview:_uSDTextField];
    
    UILabel *uSDCNY = [[UILabel alloc] initWithFrame:CGRectMake(uSDlabel.frame.size.width+uSDlabel.frame.origin.x+10, _uSDTextField.frame.origin.y+25, SCREEN_WIDTH-(uSDlabel.frame.size.width+uSDlabel.frame.origin.x+40), 50)];
    
    uSDCNY.text = @"美元";
    uSDCNY.textAlignment = NSTextAlignmentRight;
    uSDCNY.font = [UIFont systemFontOfSize:12.];
    uSDCNY.textColor = [UIColor lightGrayColor];
    [self.view addSubview:uSDCNY];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(10,uSDCNY.frame.size.height+uSDCNY.frame.origin.y , SCREEN_WIDTH-20, 0.5)];
    lineView1.backgroundColor = [UIColor lightGrayColor];
    lineView1.alpha = 0.7;
    [self.view addSubview:lineView1];
    
    //第三行
    UIImageView *eURImgae = [[UIImageView alloc] initWithFrame:CGRectMake(10, 84+90*3, 50, 50)];
    eURImgae.image = [UIImage imageNamed:@"EUR@2x.png"];
    [self.view addSubview:eURImgae];
    
    UILabel *eURlabel = [[UILabel alloc] initWithFrame:CGRectMake(eURImgae.frame.size.width+20, eURImgae.frame.origin.y, 100, 50)];
    eURlabel.text = @"EUR";
    eURlabel.textColor = [UIColor blackColor];
    eURlabel.font = [UIFont systemFontOfSize:25.];
    [self.view addSubview:eURlabel];
    
    _eURTextField = [[UITextField alloc] initWithFrame:CGRectMake(eURlabel.frame.size.width+eURlabel.frame.origin.x+10, eURImgae.frame.origin.y, SCREEN_WIDTH-(eURlabel.frame.size.width+eURlabel.frame.origin.x+40), 50)];
    _eURTextField.textAlignment = NSTextAlignmentRight;
    _eURTextField.keyboardType = UIKeyboardTypeNumberPad;
    //UITextField设置placeholder颜色
    _eURTextField.adjustsFontSizeToFitWidth = YES;

    _eURTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_eURNum]attributes:@{NSForegroundColorAttributeName: color}];
    _eURTextField.font = [UIFont boldSystemFontOfSize:25.];
    [_eURTextField addTarget:self action:@selector(eURUnitDelegate) forControlEvents:UIControlEventEditingChanged];
    _eURTextField.delegate = self;
    [self.view addSubview:_eURTextField];
    
    UILabel *eURCNY = [[UILabel alloc] initWithFrame:CGRectMake(uSDlabel.frame.size.width+eURlabel.frame.origin.x+10, _eURTextField.frame.origin.y+25, SCREEN_WIDTH-(eURlabel.frame.size.width+eURlabel.frame.origin.x+40), 50)];
    
    eURCNY.text = @"欧元";
    eURCNY.textAlignment = NSTextAlignmentRight;
    eURCNY.font = [UIFont systemFontOfSize:12.];
    eURCNY.textColor = [UIColor lightGrayColor];
    [self.view addSubview:eURCNY];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(10,eURCNY.frame.size.height+eURCNY.frame.origin.y , SCREEN_WIDTH-20, 0.5)];
    lineView2.backgroundColor = [UIColor lightGrayColor];
    lineView2.alpha = 0.7;
    [self.view addSubview:lineView2];


    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_cNYTextField resignFirstResponder];
    [_uSDTextField resignFirstResponder];
    [_eURTextField resignFirstResponder];
    [_changeTextField resignFirstResponder];
}

- (void)chineseUnitDelegate
{
    _uSDTextField.text = nil;
    _eURTextField.text = nil;
    _changeTextField.text = nil;
    UIColor *color = [UIColor blackColor];
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"美元"]) {
            _uSDNum = _cNYTextField.text.floatValue / tempModel.ExchangeRate.floatValue;
        _uSDTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_uSDNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:@"欧元"]) {
            _eURNum = _cNYTextField.text.floatValue / tempModel.ExchangeRate.floatValue;
            _eURTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_eURNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:_changeMoney]) {
            _changeNum = _cNYTextField.text.floatValue / tempModel.ExchangeRate.floatValue;
            _changeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_changeNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
    }
    
}

-(void)uSDUnitDelegate
{
    _cNYTextField.text = nil;
    _eURTextField.text = nil;
    _changeTextField.text = nil;

    UIColor *color = [UIColor blackColor];
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"美元"]) {
            _uSDUnit = tempModel.ExchangeRate;
        }
    }
    
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"人民币"]) {
            _cNYNum = _uSDTextField.text.floatValue*_uSDUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _cNYTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_cNYNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:@"欧元"]) {
            _eURNum = _uSDTextField.text.floatValue*_uSDUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _eURTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_eURNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:_changeMoney]) {
            _changeNum = _uSDTextField.text.floatValue*_uSDUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _changeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_changeNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
    }
}

-(void)eURUnitDelegate
{
    _cNYTextField.text = nil;
    _uSDTextField.text = nil;
    _changeTextField.text = nil;

    UIColor *color = [UIColor blackColor];
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"欧元"]) {
            _eURUnit = tempModel.ExchangeRate;
        }
    }
    
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"人民币"]) {
            _cNYNum = _eURTextField.text.floatValue*_eURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _cNYTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_cNYNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:@"美元"]) {
            _uSDNum = _eURTextField.text.floatValue*_eURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _uSDTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_uSDNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:_changeMoney]) {
            _changeNum = _eURTextField.text.floatValue*_eURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _changeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_changeNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
    }
}


-(void)changeUnitDelegate
{
    _eURTextField.text = nil;
    _cNYTextField.text = nil;
    _uSDTextField.text = nil;
    UIColor *color = [UIColor blackColor];
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:_changeMoney]) {
            _changeURUnit = tempModel.ExchangeRate;
        }
    }
    
    for (MyLocationModel *tempModel in _unitArray) {
        if ([tempModel.FullName isEqualToString:@"人民币"]) {
            _cNYNum = _changeTextField.text.floatValue*_changeURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _cNYTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_cNYNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:@"美元"]) {
            _uSDNum = _changeTextField.text.floatValue*_changeURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _uSDTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_uSDNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
        if ([tempModel.FullName isEqualToString:@"欧元"]) {
            _eURNum = _changeTextField.text.floatValue*_changeURUnit.floatValue / tempModel.ExchangeRate.floatValue;
            _eURTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f",_eURNum]attributes:@{NSForegroundColorAttributeName: color}];
        }
    }
}

-(void)leftClick
{
#pragma mark- 返回指定页面
    UIViewController *vc =[self.navigationController.viewControllers objectAtIndex:2];
    [self.navigationController popToViewController:vc animated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
